﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using Utility;
namespace Service
{
    /// <summary>
    /// 功能：Sys_RoleModule类数据访问
    /// 创建人：Wilson
    /// 创建时间：2013-1-30   
    /// </summary>
    public class Sys_RoleModuleService : IDisposable
    {
        #region 公共
        private System.ComponentModel.Component component = new System.ComponentModel.Component();

        private bool disposed = false;

        ~Sys_RoleModuleService()
        {
            Dispose(false);
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {

                if (disposing)
                {

                    component.Dispose();
                }

                disposed = true;

            }
        }
        #endregion
		#region##MapperSys_RoleModule
        public IList<Sys_RoleModule> MapperList(SqlDataReader reader)
         {
            IList<Sys_RoleModule> list = new List<Sys_RoleModule>();
            Sys_RoleModule obj=null;
            while (reader.Read())
            {
                obj = new Sys_RoleModule();
                obj.RmId = (int)reader["RmId"];
                obj.RoleId = reader["RoleId"] as int?;
                obj.ModuleId = (int)reader["ModuleId"];
                obj.CanRead = (bool)reader["CanRead"];
                obj.Others = reader["Others"] as string;
                list.Add(obj);
            }
            return list;
         }
        public Sys_RoleModule Mapper(SqlDataReader reader)
         {
            Sys_RoleModule obj=null;
            if (reader.Read())
            {
                obj = new Sys_RoleModule();
                obj.RmId = (int)reader["RmId"];
                    obj.RoleId = reader["RoleId"] as int?;
                obj.ModuleId = (int)reader["ModuleId"];
                obj.CanRead = (bool)reader["CanRead"];
                obj.Others = reader["Others"] as string;
            }
            return obj;
         }
        #endregion
        #region##通过主键ID得到Sys_RoleModule
        /// <summary>
        /// 功能：通过ID得到Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public Sys_RoleModule GetById(int rmid)
        {            
            string sql = "select * from Sys_RoleModule where RmId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            
            SqlDataReader reader=SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
            return Mapper(reader);
        }
        #endregion
		
		#region##通过主键ID删除Sys_RoleModule
        /// <summary>
        /// 功能：通过主键ID删除Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(int rmid)
		{
			int i = 0;
			string sql = "delete from Sys_RoleModule where RmId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
			return i;
		}
        /// <summary>
        /// 功能：通过主键ID删除Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(SqlTransaction transaction,int rmid)
		{
			int i = 0;
			string sql = "delete from Sys_RoleModule where RmId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
			return i;
		}
		#endregion			
		
        #region##添加Sys_RoleModule
        /// <summary>
        /// 功能：添加Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(Sys_RoleModule sys_rolemodule)
        {
            int result = 0;
            string sql = "insert into Sys_RoleModule(RmId,RoleId,ModuleId,CanRead,Others) values(@RmId,@RoleId,@ModuleId,@CanRead,@Others)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@RmId",sys_rolemodule.RmId),
				new SqlParameter("@RoleId",sys_rolemodule.RoleId),
				new SqlParameter("@ModuleId",sys_rolemodule.ModuleId),
				new SqlParameter("@CanRead",sys_rolemodule.CanRead),
				new SqlParameter("@Others",sys_rolemodule.Others)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：添加Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(SqlTransaction transaction,Sys_RoleModule sys_rolemodule)
        {
            int result = 0;
            string sql = "insert into Sys_RoleModule(RmId,RoleId,ModuleId,CanRead,Others) values(@RmId,@RoleId,@ModuleId,@CanRead,@Others)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@RmId",sys_rolemodule.RmId),
				new SqlParameter("@RoleId",sys_rolemodule.RoleId),
				new SqlParameter("@ModuleId",sys_rolemodule.ModuleId),
				new SqlParameter("@CanRead",sys_rolemodule.CanRead),
				new SqlParameter("@Others",sys_rolemodule.Others)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##得到Sys_RoleModule集合
		/// <summary>
        /// 功能：得到Sys_RoleModule集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>       
        /// <returns></returns>
		public IList<Sys_RoleModule> GetList()
		{
			string sql = "select * from Sys_RoleModule";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql);
            return MapperList(reader);
		}
		#endregion
		
		#region##修改Sys_RoleModule
        /// <summary>
        /// 功能：修改Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModSys_RoleModule(Sys_RoleModule sys_rolemodule)
        {
            int result = 0;
            string sql = "update Sys_RoleModule set RmId = @RmId,RoleId = @RoleId,ModuleId = @ModuleId,CanRead = @CanRead,Others = @Others where RmId = @RmId";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@RmId",sys_rolemodule.RmId),
				new SqlParameter("@RoleId",sys_rolemodule.RoleId),
				new SqlParameter("@ModuleId",sys_rolemodule.ModuleId),
				new SqlParameter("@CanRead",sys_rolemodule.CanRead),
				new SqlParameter("@Others",sys_rolemodule.Others)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：修改Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModSys_RoleModule(SqlTransaction transaction,Sys_RoleModule sys_rolemodule)
        {
            int result = 0;
            string sql = "update Sys_RoleModule set RmId = @RmId,RoleId = @RoleId,ModuleId = @ModuleId,CanRead = @CanRead,Others = @Others where RmId = @RmId";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@RmId",sys_rolemodule.RmId),
				new SqlParameter("@RoleId",sys_rolemodule.RoleId),
				new SqlParameter("@ModuleId",sys_rolemodule.ModuleId),
				new SqlParameter("@CanRead",sys_rolemodule.CanRead),
				new SqlParameter("@Others",sys_rolemodule.Others)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##统计Sys_RoleModule
		/// <summary>
        /// 修改Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			int i = 0;
            string sql = "select count(*) from Sys_RoleModule";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
		}
		/// <summary>
        /// 修改Sys_RoleModule
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            int i=0;
            string sql="select count(RmId) from Sys_RoleModule";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
        }
		#endregion
        #region##自定义Sys_RoleModule
        public IList<Sys_RoleModule> GetByRole(int roleId)
        {
            IList<Sys_RoleModule> list = new List<Sys_RoleModule>();
            Sys_RoleModule obj = null;
            string sql = "select * from Sys_RoleModule where RoleId=@roleId";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@roleId",roleId),
            };
            SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString, CommandType.Text, sql, sp);
            while (reader.Read())
            {
                obj = new Sys_RoleModule();
                obj.RmId = (int)reader["RmId"];
                obj.RoleId = reader["RoleId"] is DBNull?0:Convert.ToInt32(reader["RoleId"]);
                obj.ModuleId = (int)reader["ModuleId"];
                obj.CanRead = (bool)reader["CanRead"];
                obj.Others = reader["Others"] is DBNull ? "{}" : reader["Others"].ToString();
                list.Add(obj);
            }
            return list;
        }
        #endregion
                
    }
}
